import { createRouter, createWebHashHistory } from "vue-router";
import mapview from "@/views/mapview/index.vue";
import loginview from "@/views/login/login.vue";
import videoview from "@/views/videoview/index.vue";
import shopview from "@/views/shopview/index.vue";
import shopcarview from "@/views/shopview/shopCar.vue";
import userview from "@/views/personview/userview/index.vue";
import rolesview from "@/views/personview/rolesview/index.vue";
import menuview from "@/views/systemview/menuview.vue";
import noFound from "@/views/index/404.vue";
import indexview from "@/views/indexview.vue";
import { routeStore } from "@/store/index";

const routes = [
  {
    path: "/",
    redirect: "/login",
  },

  {
    path: "/index",
    name: "indexview",
    component: indexview,
    meta: {
      keepAlive: true,
      title: "工作台",
      icon: "VideoCamera",
    },
  },
  {
    path: "/video",
    name: "videoview",
    meta: {
      keepAlive: true,
      title: "视频管理",
      icon: "VideoCamera",
    },
    children: [
      {
        path: "/video/videoview",
        name: "videoview",
        component: videoview,
        meta: {
          keepAlive: true,
          title: "视频页面",
        },
      },
    ],
  },
  {
    path: "/map",
    name: "mapview",
    meta: {
      keepAlive: true,
      title: "地图管理",
      icon: "Guide",
    },
    children: [
      {
        path: "/map/mapview",
        name: "mapview",
        component: mapview,
        meta: {
          keepAlive: true,
          title: "地图页面",
        },
      },
    ],
  },
  {
    path: "/shop",
    name: "shopview",
    meta: {
      keepAlive: true,
      title: "购物管理",
      icon: "VideoCamera",
    },
    children: [
      {
        path: "/shop/shopview",
        name: "shopview",
        component: shopview,
        meta: {
          keepAlive: true,
          title: "购物页面",
        },
      },
      {
        path: "/shop/shopcarview",
        name: "shopcarview",
        component: shopcarview,
        meta: {
          keepAlive: true,
          title: "购物车页面",
        },
      },
    ],
  },
  {
    path: "/person",
    name: "personview",
    meta: {
      keepAlive: true,
      title: "用户管理",
      icon: "UserFilled",
    },
    children: [
      {
        path: "/person/userview",
        name: "userview",
        component: userview,
        meta: {
          keepAlive: true,
          title: "用户管理",
        },
      },
      {
        path: "/person/rolesview",
        name: "rolesview",
        component: rolesview,
        meta: {
          keepAlive: true,
          title: "角色管理",
        },
      },
    ],
  },
  {
    path: "/system",
    name: "systemview",
    meta: {
      keepAlive: true,
      title: "系统管理",
      icon: "VideoCamera",
    },
    children: [
      {
        path: "/system/menuview",
        name: "menuview",
        component: menuview,
        meta: {
          keepAlive: true,
          title: "菜单管理",
        },
      },
    ],
  },
  // 其他路由配置
  {
    path: "/login",
    name: "login",
    component: loginview,
    meta: {
      keepAlive: false,
      title: "登录",
    },
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: noFound,
    meta: {
      keepAlive: false,
      title: "404页面",
    },
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
const checkIfUserIsLoggedIn = () => {
  let token = localStorage.getItem("token");
  return token;
};
const whiteList = ["/login"];
router.beforeEach((to, from, next) => {
  console.log(to, "to");

  if (checkIfUserIsLoggedIn()) {
    if (to.path == "/login") {
      next("/index");
    } else {
      next();
    }
    console.log("你好");
    //在这写判断用户信息的逻辑 如果 无用户信息  或路由
    //调接口获取 用户信息和路由
  } else {
    if (whiteList.includes(to.path)) {
      next();
    } else {
      //   getmenuList()
      //     .then((res) => {
      //       console.log(res, "res");
      //       routeStore().routePath = res.data;
      //       next();
      //     })
      //     .catch((error) => {
      //       console.error("Error fetching menu:", error);
      //       next();
      //     });
      next("/login");
    }
  }
  // if (to.path == "/") {
  //   next("/login");
  // }
  const isLoggedIn = checkIfUserIsLoggedIn(); // 检查用户是否已登录的函数
  //  if(whiteList.includes(to.path)){
  //      next()
  //   } else if (to.meta.keepAlive && !isLoggedIn) {
  //     next('/login');
  //   } else if(to.meta.keepAlive && isLoggedIn){
  //     next()
  //   }
});

export default router;
